package com.example.switchingnetwork.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetSwitchUtil {

    /**
     * 判断WiFi是否打开
     * @param context 上下文
     * @return true is connected / false is unconnected
     */
   public static boolean isWifiConnected(Context context) {
       ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
       NetworkInfo wifiNetworkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
       return wifiNetworkInfo.isConnected();
   }

    /**
     * 判断数据流量是否打开
     * 在wifi打开时，无法通过此方法来判断数据流量开关是否打开
     * wifi优先级高于4G
     * @param context 上下文
     * @return true is connected / false is unconnected
     */
   public static boolean isMobileDataConnected(Context context) {
       ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
       NetworkInfo mobileDataNetworkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
       return mobileDataNetworkInfo.isConnected();
   }

}
